<?php include_once 'application/libraries/ctype/views/types/includes/categories_utils.php';?>

<div id="<?= $name;?>" class="ctype-field">

<?php $conf_label = array(); ?>
<?php if(form_error($name)): ?>
	<?php $conf_label['class'] = "input_error"; ?>
<?php endif; ?>
		
<?php $title_label = $properties['title']; ?>
<?php if(isset($required) && $required): ?>
	<?php $title_label .=  " *"; ?>
<?php endif; ?>

<?php $value = set_value($name, $value); ?>

<?php $this->ci->load->view("templates/admin-simpla/utils/categories", array("field_name" => $name, "field_title" => $title_label, "categories" => $categories, "selected_categories" => isset($selected_categories)?$selected_categories:array())); ?>

</div>

<script type="text/javascript">
	$(document).ready(function(){

		//Al actualizar estas categorias tendremos que actualizar las categorias generales tb
		$("#<?= $name;?> :checkbox").change(function(){
			$this = $(this);
			
			var valor = $this.attr("value");

			var checked = false;
			if($this.is(":checked")){
				checked = true;
			}
			
			if(checked){				
				$("#ctypes_categories :checkbox[value="+valor+"]").attr("checked", "checked");
			} else{
				$("#ctypes_categories :checkbox[value="+valor+"]").removeAttr("checked");
			}
		});

		//Al actualizar las categorias generales tendremos que actualizar tb estas
		$("#ctypes_categories :checkbox").change(function(){
			$this = $(this);
			
			var valor = $this.attr("value");

			var checked = false;
			if($this.is(":checked")){
				checked = true;
			}
			
			if(checked){
				$("#<?= $name;?> :checkbox[value="+valor+"]").attr("checked", "checked");
			} else{
				$("#<?= $name;?> :checkbox[value="+valor+"]").removeAttr("checked");
			}
		});







		
		//Al actualizar estas categorias tendremos que actualizar las categorias generales tb
		$("#select_categories_<?= $name;?>").change(function(){
			$(this).children("option").each(function(pos, el){
				var valor = $(el).attr("value");

				var selected = false;
				if($(el).is(":selected")){
					selected = true;
				}
				
				if(selected){
					$("#ctypes_categories option[value="+valor+"]").attr("selected", "selected");
				} else{
					$("#ctypes_categories option[value="+valor+"]").removeAttr("selected");
				}
			});
		});

		//Al actualizar las categorias generales tendremos que actualizar tb estas
		$("#ctypes_categories").change(function(){
			$(this).children("option").each(function(pos, el){
				var valor = $(el).attr("value");

				var selected = false;
				if($(el).is(":selected")){
					selected = true;
				}
				
				if(selected){
					$("#select_categories_<?= $name;?> option[value="+valor+"]").attr("selected", "selected");
				} else{
					$("#select_categories_<?= $name;?> option[value="+valor+"]").removeAttr("selected");
				}
			});
		});

	});
</script>
